﻿package mortal.common.preLoadPage
{
    import com.gengine.manager.*;
    import com.gengine.resource.core.*;
    import com.gengine.utils.*;
    import extend.language.*;
    import extend.php.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.mvc.core.*;

    public class GameLoadBar extends Sprite
    {
        public var startTime:int;
        public var nowTime:int;
        private var _timerText:TextField;
        public var gameRoot:Stage;
        private var _blackBg:Bitmap;
        private var _bg:Loader;
        private var _middlePart:Sprite;
        private var _barPart:MovieClip;
        private var _btnVoiceOpen:SimpleButton;
        private var _btnVoiceClose:SimpleButton;
        private var _totalText:TextField;
        private var _tips:TextField;
        private var _tipsTimer:Timer;
        public var isGameLoadComplete:Boolean = false;
        public var isAdded:Boolean = false;
        private var _lastTotalProgress:int = 0;
        private static var _instance:GameLoadBar;
        public static var resourceLoader:Loader;

        public function GameLoadBar()
        {
            this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
            return;
        }// end function

        private function onAddedToStage(event:Event) : void
        {
            this.stage.scaleMode = StageScaleMode.NO_SCALE;
            this.stage.align = StageAlign.TOP_LEFT;
            this.stageResizeHandler();
            this.gameRoot.addEventListener(Event.RESIZE, this.stageResizeHandler);
            return;
        }// end function

        public function initLoaderBar() : void
        {
            var textFormat:TextFormat;
            var barX:int;
            var barY:int;
            var text2:TextField;
            var splitUrl:Array;
            this._blackBg = new Bitmap(new BitmapData(1, 1, false, 0));
            this.addChildAt(this._blackBg, 0);
            this._bg = new ClientLoader(true);
            this._bg.contentLoaderInfo.addEventListener(Event.COMPLETE, this.bgCompleteHandler);
            var url:* = ParamsConst.instance.preLoadPageBg;
            if (url == null || url == "")
            {
                url;
            }
            else if (ParamsConst.instance.proxyType == ProxyType.TW)
            {
                if (ParamsConst.instance.player_id == 0)
                {
                    url = ParamsConst.instance.preLoadPageBgCreateRole;
                }
            }
            else if (ParamsConst.instance.isNewServer == 1)
            {
                splitUrl = url.split(".");
                url = splitUrl[0] + "_newServer." + splitUrl[1];
            }
            url = PathConst.mainPath + url + "?v=" + ParamsConst.instance.preLoaderVersion;
            this._bg.load(new URLRequest(url));
            this.addChildAt(this._bg, 1);
            this._middlePart = new Sprite();
            this.addChild(this._middlePart);
            textFormat = new TextFormat("微软雅黑,Microsoft YaHei,宋体");
            barX;
            barY;
            this._totalText = new TextField();
            this._totalText.x = barX;
            this._totalText.y = barY;
            this._totalText.width = 635;
            this._totalText.height = 20;
            this._totalText.textColor = 5818367;
            this._totalText.filters = [FilterConst.glowFilter];
            this._totalText.autoSize = TextFieldAutoSize.RIGHT;
            this._totalText.defaultTextFormat = textFormat;
            this._totalText.text = PreLanguage.getString(107);
            this._middlePart.addChild(this._totalText);
            this._barPart = this.getPreLoadBarMc("preLoadPageNew");
            this._barPart.y = barY;
            this._barPart.x = barX;
            this._barPart.gotoAndStop(0);
            this._middlePart.addChild(this._barPart);
            this._btnVoiceOpen = this.getBtn("voiceOpen");
            this._btnVoiceOpen.y = int(this._barPart.y + 15);
            this._btnVoiceOpen.x = this._barPart.x + 662;
            this._middlePart.addChild(this._btnVoiceOpen);
            this._btnVoiceClose = this.getBtn("voiceClose");
            this._btnVoiceClose.y = int(this._barPart.y + 15);
            this._btnVoiceClose.x = this._barPart.x + 662;
            this._btnVoiceClose.visible = false;
            this._middlePart.addChild(this._btnVoiceClose);
            this._btnVoiceOpen.addEventListener(MouseEvent.CLICK, this.onClickBtn);
            this._btnVoiceClose.addEventListener(MouseEvent.CLICK, this.onClickBtn);
            this._tips = new TextField();
            this._tips.filters = [FilterConst.glowFilter];
            this._tips.x = this._barPart.x + 140;
            this._tips.y = this._barPart.y - 5;
            this._tips.width = 500;
            this._tips.height = 25;
            this._tips.textColor = 16777215;
            this._tips.defaultTextFormat = new TextFormat("微软雅黑,Microsoft YaHei,宋体", 14);
            this._middlePart.addChild(this._tips);
            text2 = new TextField();
            text2.filters = [FilterConst.glowFilter];
            text2.textColor = 5570304;
            text2.x = 500;
            text2.y = barY + 47;
            text2.width = 300;
            text2.height = 20;
            text2.selectable = false;
            text2.autoSize = TextFieldAutoSize.RIGHT;
            text2.defaultTextFormat = textFormat;
            text2.htmlText = PreLanguage.getString(109);
            this._middlePart.addChild(text2);
            var textGameTip:* = new TextField();
            textGameTip.filters = [FilterConst.glowFilter];
            textGameTip.textColor = 16777215;
            textGameTip.x = 100;
            textGameTip.y = 560;
            textGameTip.width = 800;
            textGameTip.height = 20;
            textGameTip.selectable = false;
            textGameTip.autoSize = TextFieldAutoSize.CENTER;
            textGameTip.defaultTextFormat = textFormat;
            textGameTip.htmlText = PreLanguage.getString(153);
            this._middlePart.addChild(textGameTip);
            with ({})
            {
                {}.link = function (event:Event) : void
            {
                BrowerUtil.reload();
                return;
            }// end function
            ;
            }
            text2.addEventListener(TextEvent.LINK, function (event:Event) : void
            {
                BrowerUtil.reload();
                return;
            }// end function
            );
            GametipsConfig.instance.load(new URLRequest(PathConst.mainPath + ParamsConst.instance.gameTipsPath + "?v=" + ParamsConst.instance.flashVersion), this.onTipsLoaded);
            this.stageResizeHandler();
            return;
        }// end function

        private function bgCompleteHandler(event:Event) : void
        {
            this._bg.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.bgCompleteHandler);
            BrowerManager.instance.asCallJsFun("colseLoading");
            this.show();
            this.stageResizeHandler();
            this.dispatchEvent(new Event(Event.COMPLETE));
            return;
        }// end function

        private function onTipsLoaded() : void
        {
            if (this._tips)
            {
                this.startTips();
                this.changeTipsText();
            }
            return;
        }// end function

        public function startTips() : void
        {
            if (!this._tipsTimer)
            {
                this._tipsTimer = new Timer(GametipsConfig.instance.intervalTime);
                this._tipsTimer.addEventListener(TimerEvent.TIMER, this.onTipsTimerEvent);
                this._tipsTimer.start();
            }
            return;
        }// end function

        private function onTipsTimerEvent(event:TimerEvent) : void
        {
            this.changeTipsText();
            return;
        }// end function

        private function changeTipsText() : void
        {
            var _loc_1:String = null;
            if (this._tips)
            {
                if (ParamsConst.instance.newUser)
                {
                    _loc_1 = GametipsConfig.instance.getTip();
                }
                else
                {
                    _loc_1 = GametipsConfig.instance.getRandomTip();
                }
                if (_loc_1 != "")
                {
                    this._tips.htmlText = PreLanguage.getString(111) + _loc_1;
                }
            }
            return;
        }// end function

        public function show() : void
        {
            if (this.gameRoot && !this.gameRoot.contains(this))
            {
                this.gameRoot.addChild(this);
                this.isAdded = true;
            }
            return;
        }// end function

        public function hide() : void
        {
            if (ParamsConst.instance.newUser)
            {
                PHPSender.sendToURLByPHP(SenderType.LoadBarHide);
            }
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            this.dispose();
            return;
        }// end function

        public function getBitmapData(param1:String) : BitmapData
        {
            var _loc_2:Class = null;
            if (resourceLoader)
            {
                _loc_2 = resourceLoader.contentLoaderInfo.applicationDomain.getDefinition(param1) as Class;
                return new _loc_2(0, 0) as BitmapData;
            }
            return new BitmapData(0, 0);
        }// end function

        public function getBtn(param1:String) : SimpleButton
        {
            var _loc_2:Class = null;
            if (resourceLoader)
            {
                if (resourceLoader.contentLoaderInfo.applicationDomain.hasDefinition(param1))
                {
                    _loc_2 = resourceLoader.contentLoaderInfo.applicationDomain.getDefinition(param1) as Class;
                    return new _loc_2 as SimpleButton;
                }
                return new SimpleButton();
            }
            return new SimpleButton();
        }// end function

        public function getPreLoadBarMc(param1:String) : MovieClip
        {
            var _loc_2:Class = null;
            if (resourceLoader)
            {
                if (resourceLoader.contentLoaderInfo.applicationDomain.hasDefinition(param1))
                {
                    _loc_2 = resourceLoader.contentLoaderInfo.applicationDomain.getDefinition(param1) as Class;
                    return new _loc_2 as MovieClip;
                }
                return new MovieClip();
            }
            return new MovieClip();
        }// end function

        public function setProgress(param1:int, param2:Number, param3:Number, param4:String = "") : void
        {
            var _loc_5:* = PreloaderConfig.config;
            var _loc_6:* = _loc_5[param1]["length"];
            var _loc_7:* = _loc_5[param1]["baseValue"];
            var _loc_8:* = _loc_5[param1]["text"];
            var _loc_9:* = Math.round(_loc_7 + param2 / param3 * _loc_6);
            if (_loc_9 < this._lastTotalProgress)
            {
                return;
            }
            this._lastTotalProgress = _loc_9;
            if (this._totalText)
            {
                this._totalText.text = PreLanguage.getString(107) + _loc_9 + "%";
            }
            if (this._barPart)
            {
                _loc_9 = Math.round(200 * param2 / param3 % 101);
                this._barPart.gotoAndStop(_loc_9);
            }
            return;
        }// end function

        private function stageResizeHandler(event:Event = null) : void
        {
            if (this._blackBg)
            {
                this._blackBg.width = this.gameRoot.stageWidth;
                this._blackBg.height = this.gameRoot.stageHeight;
            }
            if (this._middlePart)
            {
                this._middlePart.x = int(this.gameRoot.stageWidth / 2 - 500);
                this._middlePart.y = int(this.gameRoot.stageHeight / 2 - 280);
            }
            if (this._bg && this._bg.content)
            {
                this._bg.x = (this.gameRoot.stageWidth - this._bg.content.width) / 2;
                this._bg.y = (this.gameRoot.stageHeight - this._bg.content.height) / 2 - 10;
            }
            return;
        }// end function

        protected function onClickBtn(event:MouseEvent) : void
        {
            ParamsConst.instance.isEnterSetVoice = true;
            switch(event.currentTarget)
            {
                case this._btnVoiceOpen:
                {
                    this._btnVoiceClose.visible = true;
                    this._btnVoiceOpen.visible = false;
                    ParamsConst.instance.isEnterNoVoice = true;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.LoginVoiceSet, true));
                    break;
                }
                case this._btnVoiceClose:
                {
                    this._btnVoiceClose.visible = false;
                    this._btnVoiceOpen.visible = true;
                    ParamsConst.instance.isEnterNoVoice = false;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.LoginVoiceSet, false));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function dispose() : void
        {
            if (this._btnVoiceClose)
            {
                this._btnVoiceClose.removeEventListener(MouseEvent.CLICK, this.onClickBtn);
                this._btnVoiceClose = null;
            }
            if (this._btnVoiceOpen)
            {
                this._btnVoiceOpen.removeEventListener(MouseEvent.CLICK, this.onClickBtn);
                this._btnVoiceOpen = null;
            }
            if (this._blackBg && this._blackBg.parent)
            {
                this._blackBg.parent.removeChild(this._blackBg);
                this._blackBg.bitmapData.dispose();
                this._blackBg = null;
            }
            if (this._tips)
            {
                if (this._tips.parent)
                {
                    this._tips.parent.removeChild(this._tips);
                }
                this._tips = null;
            }
            if (this._tipsTimer)
            {
                this._tipsTimer.stop();
                this._tipsTimer = null;
            }
            _instance = null;
            if (resourceLoader)
            {
                resourceLoader.unload();
                resourceLoader = null;
            }
            this._lastTotalProgress = 0;
            if (this._bg)
            {
                this._bg.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.bgCompleteHandler);
                this._bg.unload();
                this._bg = null;
            }
            return;
        }// end function

        public static function get instance() : GameLoadBar
        {
            if (!_instance)
            {
                _instance = new GameLoadBar;
            }
            return _instance;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
